<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="摘要" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="abst">
               <a-textarea
                rows="4"
                placeholder="请输入摘要"
                v-model="model.abst"
                :maxLength="maxRemnant"
              />
              <span style="position: absolute;bottom: -35px;right: 6px;font-family: PingFangSC-Regular;font-size: 12px;color: #c1c1c1;letter-spacing: 0;font-weight: 400;">
                {{remnant}}/{{maxRemnant}}</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="缩略图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <j-image-upload isMultiple v-model="model.img" bizPath="web"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="publicTime">
              <a-date-picker placeholder="请选择发布时间" v-model="model.publicTime" show-time format="YYYY/MM/DD HH:mm:ss" style="width: 100%;" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="浏览次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="view">
              <a-input-number v-model="model.view" placeholder="请输入浏览次数" style="width: 100%;" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="点赞次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zan">
              <a-input v-model="model.zan" placeholder="请输入点赞次数"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="置顶" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="toping">
              <j-dict-select-tag type="radio" v-model="model.toping" dictCode="yesno" placeholder="请选择是否置顶" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag
                type="list"
                v-model="model.status"
                dictCode="cms_news_status"
                placeholder="请选择状态"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CmsNewsForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      remnant:0,
      maxRemnant:500,
      model: {
        publicTime:new Date(),
        view:100,
        toping:0,
        status:1,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        title: [{ required: true, message: '请输入标题' }],
        abst: [{ required: true, message: '请输入摘要' }],
        view: [{ required: true, message: '请输入浏览次数' }], 
        toping: [{ required: true, message: '请选择是否置顶' }],
        status: [{ required: true, message: '请输选择状态' }]
      },
      url: {
        add: '/corp/news/add',
        edit: '/corp/news/edit',
        queryById: '/corp/news/queryById'
      }
    }
  },
  watch:{
    "model.abst"(val){
      if(val){
        this.remnant = val.length;
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
